package servlet;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.FileClassDao;
import dao.FileDao;
import dao.impl.FileClassDaoImpl;
import dao.impl.FileDaoImpl;
import domian.File;
import domian.FileClassName;
import domian.Paging;
import redis.clients.jedis.Jedis;
import service.FileClassService;
import service.IndexService;
import service.impl.FileClassServiceImpl;
import service.impl.IndexServiceImpl;
import utils.ToolSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/indexServlet/*")
public class IndexServlet extends BaseServlet{

    public void headlineShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        FileClassDao fcd = new FileClassDaoImpl();
        FileClassService fcs = new FileClassServiceImpl();
//
//        //按招一级标题二级标题将表题分类好,返回成一个list集合
//
//        List<FileClassName> classByCid = fcd.findClassByCid();
//
//        ToolSet.printWriter(resp,classByCid);

        Jedis jedis = new Jedis();
        ObjectMapper om = new ObjectMapper();

        String json = null;

        Boolean titleList = jedis.exists("TitleList");
        if (titleList){
            json = jedis.get("TitleList");
        }else {
            List<FileClassName> titleBerAll = fcs.findTitle();
            //将列表转化为json字符串
            json = om.writeValueAsString(titleBerAll);
            //将json存入rdeis数据库
            jedis.set("TitleList",json);

            //设置其存活时间半个小时
            jedis.expire("TitleList",30*60*60);
        }

        //将json字符串转为list集合
        List<FileClassName> classByCid = om.readValue(json, new TypeReference<List<FileClassName>>() {});

        ToolSet.printWriter(resp,classByCid);
    }

    /**
     * 分页查询数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void pagingQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        Integer fileClass = Integer.parseInt(req.getParameter("fileClass"));    //需要查询的分类id
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));    //当前是第几页
        Integer pageSize = 12;           //页面大小

        //创建服务层对象
        IndexService ise = new IndexServiceImpl();

        //查到分页数据
        Paging paging = ise.pagingQuery(fileClass, currentPage, pageSize);

        //返回前端
        ToolSet.printWriter(resp,paging);
    }

    /**
     * 增加这个文件的浏览量
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void augmentPageView(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        Integer fid = Integer.parseInt(req.getParameter("fid"));

        FileDao fd = new FileDaoImpl();

        File file = fd.findFileById(fid);

        //将浏览量加一
        file.setF_view(file.getF_view()+1);

        //将加一后的浏览量更新至数据库
        fd.updateFile(file);

        ToolSet.writer(resp,String.valueOf(fid));
    }
}
